package com.cxs.controller;

import com.cxs.model.Users;
import com.cxs.service.UserService;
import com.cxs.utils.LayuiPageResult;
import com.github.pagehelper.PageInfo;
import cxs.utils.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @Author:cxs
 * @Date:2021/10/12  11:48
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@RestController
@RequestMapping("/user")
@Slf4j
//@CrossOrigin(
//        allowCredentials = "true",
//        methods = {
//                RequestMethod.PUT,
//                RequestMethod.DELETE,
//                RequestMethod.POST,
//                RequestMethod.GET,
//                RequestMethod.OPTIONS,
//                RequestMethod.PATCH
//        }
//)
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public LayuiPageResult list(
            @RequestParam(value = "page",required = true,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = true,defaultValue = "5") Integer pageSize,
            @RequestParam(value = "keyword",required = false) String keyword){
        PageInfo<Users> pageInfo = userService.selectList(pageNum, pageSize, keyword);
        return new LayuiPageResult(0,"请求成功",pageInfo.getTotal(),pageInfo.getList());
    }

    @PostMapping("/updatePass")
    public Result updatePass(Long id,String password){
        if(id == null || password == null){
            return Result.OK("信息有误,修改失败",203,null);
        }
        Users user = new Users();
        user.setId(id);
        user.setPassword(password.trim());
        return userService.update(user);
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        if(id == null) {
            return Result.failare("id岂能为空");
        }
        return userService.del(id);
    }

    @PostMapping("/bulkdelete")
    public Result bulkdelete(@RequestBody List<Long> ids){
        if(ids.isEmpty() || ids.size() == 0) {
            return Result.failare("ids岂能为空");
        }
        System.out.println(ids);
        return userService.blukdel(ids);
    }
}
